<template>
  <div>
    <Title title="权限设置"></Title>
    <el-row>
      <el-col :span="6">
        <el-form
          ref="formRef"
          :model="form"
          status-icon
          :rules="rules"
          label-width="120px"
          label-position="top"
          class="myform"
        >
          <el-form-item label="权限名称" prop="label">
            <el-input
              clearable
              v-model="form.label"
              placeholder="权限名称"
              type="text"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="权限价值" prop="value">
            <el-input
              clearable
              v-model="form.value"
              placeholder="权限价值"
              type="text"
              autocomplete="off"
            />
          </el-form-item>
          <el-form-item label="权限属性" prop="color">
            <el-input
              clearable
              v-model="form.color"
              placeholder="权限属性"
              type="color"
              autocomplete="off"
            />
          </el-form-item>

          <el-form-item>
            <el-button type="primary" class="btnblock" @click="submitData()"
              >权限添加</el-button
            >
            <el-button type="danger" class="btnblock" @click="resetForm()"
              >重置</el-button
            >
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script lang="ts" setup>
import { ref, reactive } from "vue";
import { useRouter } from "vue-router";
import { Ajax } from "@/api";
import { ShowErrorMsg } from "@/utils";
const router = useRouter();
const form = ref<any>({}); //表单数据
const formRef = ref(); //表单实例
const rules = reactive({
  label: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
  ],
  value: [
    {
      required: true,
      message: "请输入",
      trigger: "blur",
    },
    {
      pattern: new RegExp("^[0-9]*[1-9][0-9]*$"), // 整数
      message: "必须是整数",
      trigger: "blur",
    },
  ],
  color: [
    {
      required: true,
      message: "请选择",
      trigger: "blur",
    },
  ],
});
const submitData = () => {
  formRef.value.validate(async (valid: boolean) => {
    if (valid) {
      let res:any = await Ajax.roleadd(form.value);
      if (res.code == 200) {
        router.push({ name: "role-list" });
      }
    } else {
      ShowErrorMsg("校验失败");
    }
  });
};
const resetForm = () => {
  formRef.value.resetFields();
};
</script>
